﻿Imports Kimmo

Public Class frmProjects
    Inherits System.Web.UI.Page
    Private WithEvents btNew As FineUI.Button
    Private WithEvents btRefresh As FineUI.Button

    Private strID As String = ""
    Private strAction As String = ""
    Private okScript As String = ""
    Private strView As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.btNew = clsFineUI.ToolBar.AddClickButton(tblLeft, "New", "~/images/newsomething16.gif", "Add a new project")
        Me.btRefresh = clsFineUI.ToolBar.AddClickButton(tblLeft, "Refresh", "~/images/refresh.gif", "Refresh the projects list")
        strID = WebSite.GetQueryValue("id")
        strAction = WebSite.GetQueryValue("action")
        strView = WebSite.GetQueryValue("view")
        If Len(strID) > 0 Then clsDL.SaveSetting("id", strID)
        If Len(strAction) > 0 Then clsDL.SaveSetting("action", strAction)
        If Len(strView) > 0 Then clsDL.SaveSetting("view", strView)
        If IsPostBack Then
        Else
            Select Case strAction
                Case "delete"
                    Projects_DeleteRecords()
            End Select
        End If
        Projects_Report()
    End Sub

    Protected Sub Projects_Blank()
        Dim clsA As New clsProjects
        clsA.ID = -1
        clsA.IsNew = True
        clsA.ProjectPath = ""
        clsA.ProjectName = "New Project"
        clsA.LastUpdated = Today.Date
        clsA.UpdatedBy = ""
        clsA.Save()
        clsA.ProjectName = clsA.ProjectName & " " & clsA.ID.ToString
        clsA.Save()
        clsDL.SaveSetting("id", clsA.ID)
        clsDL.SaveSetting("view", "project")
        Projects_Report()
    End Sub

    Protected Sub Projects_DeletePrompt(ByVal StrID As String)
        clsDL.SaveSetting("deleteid", StrID)
        Me.okScript = "window.location.href='frmProjects.aspx?action=delete';"
        clsFineUI.MyConfirmation("Confirm Delete", "Are you sure that you want to delete this project? If you choose Yes, your actions cannot be undone. Delete?", Me.okScript, "")
    End Sub


    Protected Sub Projects_Save(ByRef row As FineUI.GridRow, ByVal strDK As String)
        'Dim fuUploader As System.Web.UI.WebControls.FileUpload = DirectCast(row.FindControl("filuploader"), System.Web.UI.WebControls.FileUpload)
        Dim clsP As New clsProjects
        clsP.Read(strDK)
        clsP.ProjectName = clsDL.TextBoxValue(row, "ProjectName")
        clsP.ProjectName = Files.File_CleanName(clsP.ProjectName)
        Dim strFolder As String = WebSite.BuildFolder("/", "Software Projects", clsP.ProjectName)
        clsP.ProjectPath = "~" & strFolder
        clsP.LastUpdated = Today.Date
        clsP.UpdatedBy = ""
        clsP.Save()
        clsColumns.UpdateProjectName(clsP.ID.ToString, clsP.ProjectName)
        clsTables.UpdateProjectName(clsP.ID.ToString, clsP.ProjectName)
        clsDL.SaveSetting("view", "projects")
        clsDL.SaveSetting("id", "")
        Projects_Report()
    End Sub

    Protected Sub Projects_DeleteRecords()
        strID = clsDL.ReadSetting("deleteid")
        If Len(strID) > 0 Then clsProjects.DeleteByID(strID)
        ' ensure that the delete is removed from address"
        clsDL.SaveSetting("id", "")
        clsDL.SaveSetting("view", "projects")
        WebSite.GotoPage("frmProjects.aspx")
    End Sub

    Protected Sub Projects_Report()
        Me.regLeft.Title = "Projects"
        strID = clsDL.ReadSetting("id")
        strView = clsDL.ReadSetting("view")
        Select Case strView
            Case "projects", ""
                clsProjects.ShowProjects(grdViews, "")
            Case "project"
                clsProjects.ShowProjects(grdViews, "id = " & strID)
        End Select
    End Sub

    Private Sub Projects_Clone(ByVal StrID As String)
        Dim nSV As New clsProjects()
        nSV.Read(StrID)
        nSV.ID = CLng(-1)
        nSV.ProjectName = "Copy of " & nSV.ProjectName
        nSV.Save()
        clsDL.SaveSetting("view", "project")
        clsDL.SaveSetting("id", nSV.ID.ToString)
        Projects_Report()
    End Sub

    Public Sub grdViews_PageIndexChange(sender As Object, e As FineUI.GridPageEventArgs) Handles grdViews.PageIndexChange
        Me.grdViews.PageIndex = e.NewPageIndex
    End Sub

    Public Sub grdViews_RowCommand(sender As Object, e As FineUI.GridCommandEventArgs) Handles grdViews.RowCommand
        Dim strCommand As String = e.CommandName
        Dim strDK As String = clsFineUI.Grid.RowID(grdViews, e.RowIndex, 0)
        Dim row As FineUI.GridRow = Me.grdViews.Rows(e.RowIndex)
        Select Case strCommand
            Case "save" : Me.Projects_Save(row, strDK)
            Case "clone" : Me.Projects_Clone(strDK)
            Case "delete" : Me.Projects_DeletePrompt(strDK)
            Case "table" : WebSite.GotoPage("frmTablesTree.aspx?view=tables&projectid=" & strDK)
        End Select
    End Sub

    Protected Sub btNew_Click(sender As Object, e As EventArgs) Handles btNew.Click
        Projects_Blank()
    End Sub

    Private Sub btRefresh_Click(sender As Object, e As EventArgs) Handles btRefresh.Click
        clsDL.SaveSetting("id", "")
        clsDL.SaveSetting("view", "projects")
        clsDL.SaveSetting("projectid", "")
        Projects_Report()
    End Sub
End Class